<?php

/**
 * default.php
 *
 * default application controller
 *
 * @package		TinyMVC
 * @author		Monte Ohrt
 */

class Company_Controller extends TinyMVC_Controller
{
    public function index()
    {
        $params = $_GET;
        $where = array();

        if(array_key_exists('cat', $params) && !empty($params['cat'])) {
            settype($params['cat'], 'int');
            $where[] = "company_has_category.category_id = {$params['cat']}";
        }

        if(array_key_exists('location', $params) && !empty($params['location'])) {
            settype($params['location'], 'int');
            $where[] = "company.location_id = {$params['location']}";
        }

        if(!array_key_exists('sort', $params)) {
            $params['sort'] = 'name';
        }

        if(!array_key_exists('dir', $params)) {
            $params['dir'] = 'desc';
        }

        switch($params['sort']) {
            case 'name':
                $order['field'] = 'company.name';
            break;
            case 'rating':
                $order['field'] = 'company_rating.rating';
            break;
        }

        if($params['dir'] == 'desc') {
            $order['dir'] = 'DESC';
        } else {
            $order['dir'] = 'ASC';
        }

        $companyModel = new Company_Model();
        $companyModel->db->query('SET NAMES UTF8');

        $this->view->assign('companyCollection', $companyModel->getCollection($where, $order));

        $categoryModel = new Category_Model();
        $this->view->assign('categoryCollection', $categoryModel->getCollection());

        $locationModel = new Location_Model();
        $this->view->assign('locationCollection', $locationModel->getCollection());

        $this->view->assign('params', $params);
        $this->view->display('index_view');
    }

    public function add()
    {
        $companyModel2 = new Company_Model();
        $companyModel2->db->query('SET NAMES UTF8');
        unset($companyModel2);

        if($_POST) {

            $form = $_POST;

            $companyModel = new Company_Model();
            $companyModel->db->query('SET NAMES UTF8');
            $companyModel->setData(array(
                'name' => $form['name'],
                'email' => $form['email'],
                'short_desc' => $form['short_desc'],
                'phone' => $form['phone'],
                'location_id' => (int)$form['location']
                )
            );

            if(array_key_exists('cat', $form)) {
                $companyModel->setCategory($form['cat']);
            }

            $companyModel->setRating($form['rating']);
            $companyModel->save();

            header('Location: http://redmarks.ru/htdocs/index.php/company/index/');
            exit;

        }

        $categoryModel = new Category_Model();
        $this->view->assign('categoryCollection', $categoryModel->getCollection());

        $locationModel = new Location_Model();
        $this->view->assign('locationCollection', $locationModel->getCollection());

        $this->view->display('add_view');

    }
}

?>
